<?php
/**
 * 栏目管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
UE_Loader::loadModel('CatModel', 'system');
class CategoryController extends UE_Controller_Admin {
	/**
	 * 栏目/分类模型
	 *
	 * @var CatModel
	 */
	private $_cat;
	
	public function init() {
		$this->_cat = new CatModel();
	}
	/**
	 * 栏目列表
	 *
	 */
	public function listAction() {
		$appFilter = empty($_GET['cat_app'])? '' :$_GET['cat_app'];
		
		if (IS_POST) {
			foreach ($_POST['cat_order'] as $catId => $oid) {
				$sql= "UPDATE ue_cat SET cat_order = '$oid' WHERE cat_id='$catId'";
				UE_DB::factory()->query($sql);
				UE_Cache::createCache()->clear('cats');
			}
		}
		
		$_cats = $this->_cat->getCats($appFilter);
				
	    include $this->tpl();
	}
	
	/**
	 * 添加栏目/分类
	 *
	 */
	public function addAction() {
		$appFilter = empty($_GET['cat_app'])? '' :$_GET['cat_app'];
		
		if (IS_POST) {
			//print_r($_POST);
			// 防刷新重复提交
			if (isRepost('cat_add_nrp', $_POST)) {
				$this->setWarn('该分类已经添加，不再重复添加！');
			} elseif($this->_cat->add($_POST)) {
				$this->setOK('成功添加分类');
			} else {
				$this->setErr($this->_cat->getErrs());
			}			
		}
		
		$_cats = $this->_cat->getCats($appFilter);
		$useCatApps = $this->_cat->getUseCatApps();
		
	    include $this->tpl();
	}
	
	/**
	 * 编辑栏目
	 *
	 */
	public function editAction() {
		if (empty($this->id)) {
			print "Error Param!";
			return false;
		}
		
		//$appFilter = empty($_GET['cat_app'])? '' :$_GET['cat_app'];
		$this->_cat->setId($this->id);
		if (IS_POST) {
			if($this->_cat->update($_POST)){
				$this->setOK('成功修改分类');
			} else {
				$this->setErr($this->_cat->getErrs());
			}
		}
		
		$catArr = $this->_cat->read();
		$_cats = $this->_cat->getCats($catArr['cat_app']);
				
		//print_r($catArr);
		
	    include $this->tpl();
	}
	
	/**
	 * 删除栏目
	 *
	 */
	public function delAction() {
		if (empty($this->id)) {
			print "Error Param!";
			return false;
		}
		
		$this->_cat->setId($this->id);
		
	    if(!$this->_cat->delete()) {
	    	$this->setErr($this->_cat->getErrs());
	    	$this->showMessage();
			return false;
	    }
	    
	    header('Location: '.UE::referer());
	}
	
	public function ajaxChInNavAction() {
		if (empty($this->id)) {
			print "Error Param!";
			return false;
		}
		
		$sql = "UPDATE ue_cat SET cat_nav_show = not cat_nav_show WHERE cat_id='{$this->id}'";
		UE_DB::factory()->query($sql);
		$inNav = UE_DB::factory()->getfirstField("SELECT cat_nav_show FROM ue_cat WHERE cat_id='{$this->id}'");
		UE_Cache::createCache()->clear('cats');
		
		print json_encode(array('err'=>'', 'r'=>$inNav));
	}
	
		
	public function ajaxChEnabledAction() {
		if (empty($this->id)) {
			print "Error Param!";
			return false;
		}
		
		$sql = "UPDATE ue_cat SET cat_enabled = not cat_enabled WHERE cat_id='{$this->id}'";
		UE_DB::factory()->query($sql);
		$inNav = UE_DB::factory()->getfirstField("SELECT cat_enabled FROM ue_cat WHERE cat_id='{$this->id}'");
		UE_Cache::createCache()->clear('cats');
		
		print json_encode(array('err'=>'', 'r'=>$inNav));
	}
	
	public function renewUrlAction() {
		
		if ($this->_cat->renewUrl()) {
			$this->setOK('成功更新');
		} else {
			$this->setErr($this->_cat->getErrs());
		}
		
		$this->showMessage();		
	}
}